#include "messagelog.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDateTime>
#include <QPalette>

MessageLog::MessageLog(QWidget *parent)
    : QWidget{parent}
{
    log_text = new QPlainTextEdit;
    log_text->setPlainText("");
    log_text->setReadOnly(true);
    btn_clear = new QPushButton(tr("清除"));
    QFont font("Courier");
    log_text->setFont(font);
    QVBoxLayout *layout_dig = new QVBoxLayout;
    layout_dig->addWidget(log_text);
    layout_dig->addWidget(btn_clear);
    setLayout(layout_dig);
    setWindowFlag(Qt::WindowMinMaxButtonsHint,true);
    setWindowFlag(Qt::WindowCloseButtonHint,true);
    setWindowTitle(tr("Log"));

    // log_text->setStyleSheet("background-color: lightblue;");
    QPalette palette;
    palette.setColor(QPalette::Base, QColor(30,60,70));
    log_text->setAutoFillBackground(true);
    log_text->setPalette(palette);


    connect(btn_clear,&QPushButton::clicked,this,&MessageLog::btn_click_clear);
    // connect(this,&MessageLog::append_text,log_text,&QPlainTextEdit::appendPlainText);
}

MessageLog::~MessageLog()
{
}

void MessageLog::set_text(const QString &text)
{
    log_text->setPlainText(text);
}

// void MessageLog::append_text(const QString &text)
// {
//     log_text->appendPlainText(text);
// }

void MessageLog::clear_text()
{
    log_text->clear();
}

void MessageLog::btn_click_clear()
{
    QMessageBox msgBox;
    msgBox.setText(tr("确定清除日志吗?"));
    // msgBox.setInformativeText(tr("所有EQ参数将会清零"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::No);
    msgBox.setIcon(QMessageBox::Warning);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Yes)
    {
        clear_text();
    }
}

#include <QTextBlock>
void MessageLog::append_log_text(QtMsgType type,const QString &msg)
{
    // qDebug() << "func:" << __func__ << ",Current thread ID: " << QThread::currentThreadId();
    QString head;
    //text.append(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") + " ");
    head.append(QDateTime::currentDateTime().toString("hh:mm:ss") + " ");
    //1、获取当前行（块）数
    int count = log_text->blockCount();
    //2、创建文本块
    QTextBlock block = log_text->document()->findBlockByNumber(count - 1);
    //3、设置光标位置
    log_text->setTextCursor(QTextCursor(block));
    //4、创建TextCharFormat对象
    QTextCharFormat fmt;
    //5、设置文本颜色
    fmt.setForeground(QColor(255,255,255));
    log_text->mergeCurrentCharFormat(fmt);
    log_text->appendPlainText(head);

    // head.append(QString("count:%1,  ").arg(count));
    switch(type)
    {
        case QtMsgType::QtDebugMsg://级别最低，可以随意输出的调试信息
        {
            fmt.setForeground(QColor(255,255,255));
        }
        break;
        case QtMsgType::QtInfoMsg://用于反馈系统运行状态
        {
            fmt.setForeground(QColor(0,150,200));
        }
        break;
        case QtMsgType::QtWarningMsg://警告信息，可以修复的，不影响程序运行
        {
            fmt.setForeground(QColor(255,255,0));
        }
        break;
        case QtMsgType::QtCriticalMsg://严重错误，可能导致程序无法正常运行
        {
            fmt.setForeground(QColor(255,0,0));
        }
        break;
        case QtMsgType::QtFatalMsg://致命错误，程序无法正常运行
        {
            fmt.setForeground(QColor(255,0,0));
        }
        break;
        default:
        {
            fmt.setForeground(QColor(255,255,255));
        }
        break;
    }
    log_text->moveCursor(QTextCursor(block).End);
    log_text->mergeCurrentCharFormat(fmt);
    log_text->insertPlainText(msg);

    ////

}


